package com.onluy.frameworkcloud.system.common;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;


public class ParameterRequestWrapper extends HttpServletRequestWrapper {

	protected Logger log =   LoggerFactory.getLogger(this.getClass());

	private Map<String, String[]> params;
	
	private final byte[] body;
	
	public ParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> newParams) {
		super(request);
		this.params = newParams;
		body = readBytes(request, "utf-8");
	}

	@Override
	public String getParameter(String name) {
		String result = "";

		Object v = params.get(name);
		if (v == null) {
			result = null;
		} else if (v instanceof String[]) {
			String[] strArr = (String[]) v;
			if (strArr.length > 0) {
				result = strArr[0];
			} else {
				result = null;
			}
		} else if (v instanceof String) {
			result = (String) v;
		} else {
			result = v.toString();
		}

		return result;
	}
	
	 @Override    
	 public BufferedReader getReader() throws IOException {    
	        return new BufferedReader(new InputStreamReader(getInputStream()));
	 }

	  @Override    
	  public ServletInputStream getInputStream() throws IOException {    
	      final ByteArrayInputStream bais = new ByteArrayInputStream(body);
	      return new ServletInputStream() {
	    
	            @Override    
	            public int read() throws IOException {    
	                return bais.read();    
	            }    
	        };    
	    }   
	
	@Override
	public Map<String, String[]> getParameterMap() {
		return params;
	}

	@Override
	public Enumeration<String> getParameterNames() {
		return new Vector<String>(params.keySet()).elements();
	}

	@Override
	public String[] getParameterValues(String name) {
		String[] result = null;

		Object v = params.get(name);
		if (v == null) {
			result = null;
		} else if (v instanceof String[]) {
			result = (String[]) v;
		} else if (v instanceof String) {
			result = new String[] { (String) v };
		} else {
			result = new String[] { v.toString() };
		}

		return result;
	}
	
	/** 
     * 通过BufferedReader和字符编码集转换成byte数组 
     * @param request
     * @param encoding 
     * @return 
     * @throws IOException  
     */  
    private byte[] readBytes(HttpServletRequest request,String encoding){  
    	StringBuffer json = new StringBuffer();
		   String line = null;
		   try {
			   BufferedReader reader = request.getReader();
			   while((line = reader.readLine()) != null) {
				   		json.append(line);
			   		}
		   		}
		   catch(Exception e) {
			   log.info(e.getMessage());
		   }
        return json.toString().getBytes( Charset.forName(encoding));
    }  
}